using System;
using System.Collections.Generic;
using System.Text;

namespace SDT.Libraries.Utilities
{
    public class StringReplacer
    {
        #region Members
        Dictionary<string, string> stringReplacementPairs = new Dictionary<string, string>();
        string content = string.Empty;
        bool ignoreCase = false;
        #endregion

        #region Properties
        public bool IgnoreCase
        {
            get { return ignoreCase; }
            set { ignoreCase = value; }
        }
        #endregion

        #region Constructors
        public StringReplacer(string content)
        {
            this.content = content;
        }

        public StringReplacer()
        {
        }
        #endregion

        #region Public methods
        public void SetContent(string content)
        {
            this.content = content;
        }

        public void AddStringReplacementPair(string stringToFind, string stringToReplace)
        {
            stringReplacementPairs.Add(stringToFind, stringToReplace);
        }

        public void ClearPairs()
        {
            stringReplacementPairs.Clear();
        }

        public string GetReplacedContent()
        {
            string replacedContent = content;
            foreach (KeyValuePair<string, string> pair in stringReplacementPairs)
            {
                replacedContent = Replace(replacedContent, pair.Key, pair.Value, ignoreCase);
            }

            return replacedContent;
        }
        #endregion

        #region Static method
        public static string Replace(string content, string stringToFind, string stringToReplace)
        {
            return Replace(content, stringToFind, stringToReplace, false);
        }

        public static string Replace(string content, string stringToFind, string stringToReplace, bool ignoreCase)
        {
            if (content == null)
            {
                return null;
            }

            if (String.IsNullOrEmpty(stringToFind))
            {
                return content;
            }

            StringComparison comparisonType = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
            int lenPattern = stringToFind.Length;
            int idxPattern = -1;
            int idxLast = 0;

            StringBuilder result = new StringBuilder();
            while (true)
            {
                idxPattern = content.IndexOf(stringToFind, idxPattern + 1, comparisonType);
                if (idxPattern < 0)
                {
                    result.Append(content, idxLast, content.Length - idxLast);
                    break;
                }

                if (idxLast > idxPattern)
                    result.Remove(result.Length - stringToReplace.Length, stringToReplace.Length);
                else
                    result.Append(content, idxLast, idxPattern - idxLast);

                result.Append(stringToReplace);
                idxLast = idxPattern + lenPattern;
            }

            return result.ToString();
        }
        #endregion
    }
}
